<template>
    <div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                 class="demo-ruleForm" style="width: 30%">
            <el-form-item label="旧密码" prop="oldPassword">
                <el-input type="password" v-model="ruleForm.oldPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
                <el-input type="password" v-model="ruleForm.newPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="retypeNewPassword">
                <el-input type="password" v-model="ruleForm.retypeNewPassword" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item align="center">
                <el-button type="primary" @click="submitForm('ruleForm')" size="mini">提交</el-button>
                <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {updatePassword} from "@/api/roleApi";

    export default {
        name: "pwdManage",
        data() {
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    oldPassword: null,
                    newPassword: null,
                    retypeNewPassword: null
                },
                rules: {
                    oldPassword: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    newPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 5, max: 12, message: '长度在 5 到 12个字符'},
                        {pattern: /^(\w){5,12}$/, message: '只能输入5-25个字母、数字、下划线'}
                    ],
                    retypeNewPassword: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            //修改密码
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url = `oldPassword=${this.ruleForm.oldPassword}&newPassword=${this.ruleForm.newPassword}&retypeNewPassword=${this.ruleForm.retypeNewPassword}`;
                        updatePassword(url).then(res => {
                            this.$message.success("修改成功");
                        })
                    } else {
                        return false;
                    }
                });
            },
            // 重置
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        }
    }
</script>

<style scoped>

</style>